package com.sit.estatemanagementserver.controller;

import com.sit.estatemanagementserver.enity.Complaint;
import com.sit.estatemanagementserver.enity.Response;
import com.sit.estatemanagementserver.service.ComplaintService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/complaints") // 重构: 路径改为复数形式，更符合REST规范
public class ComplaintController {
    @Autowired
    private ComplaintService complaintService;

    //提交投诉
    @PostMapping("/add")
    // 重构: 返回类型改为Response<?>，由框架自动序列化
    public Response<?> submitComplaint(@RequestBody Complaint complaint) {
        if (complaintService.addComplaint(complaint)) {
            return Response.success();
        } else {
            return Response.error("投诉建议提交失败！");
        }
    }

    @GetMapping("/owner/{ownerPhone}")
    public Response<List<Complaint>> getComplaintsByOwner(@PathVariable String ownerPhone) {
        List<Complaint> complaints = complaintService.getComplaintsByOwner(ownerPhone);
        if (complaints.isEmpty()) {
            return Response.error("没有投诉建议");
        } else {
            return Response.success(complaints);
        }
    }
}